package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Client;
import cn.wolfcode.wms.mapper.ClientMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    public void deleteById(Long id) {
        clientMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Client client) {
        if (client.getId() != null) {
            clientMapper.updateByPrimaryKey(client);
        } else {
            clientMapper.insert(client);
        }
    }

    public Client getById(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = clientMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Client> data = clientMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void deleteall(Long[] ids) {
        if (ids!=null){
            for (Long id : ids) {
                clientMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
